

class AreaDeInteresseController {

    def beforeInterceptor = [action:this.&auth, except:['login', 'autenticacao', 'logout']]

    def auth() {
        if (session.usuario) {
            if (session.usuario.nivelDeAcesso == Usuario.NIVEL_ADMINISTRADOR) return true
        }

        flash.message = "Você não está logado ou não tem permissão para visualizar essa página"
        redirect(controller:'usuario', action:'login')
        return false
    }

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ areaDeInteresseInstanceList: AreaDeInteresse.list( params ), areaDeInteresseInstanceTotal: AreaDeInteresse.count() ]
    }

    def show = {
        def areaDeInteresseInstance = AreaDeInteresse.get( params.id )

        if(!areaDeInteresseInstance) {
            flash.message = "AreaDeInteresse not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ areaDeInteresseInstance : areaDeInteresseInstance ] }
    }

    def delete = {
        def areaDeInteresseInstance = AreaDeInteresse.get( params.id )
        if(areaDeInteresseInstance) {
            try {
                areaDeInteresseInstance.delete()
                flash.message = "AreaDeInteresse ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "AreaDeInteresse ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "AreaDeInteresse not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def areaDeInteresseInstance = AreaDeInteresse.get( params.id )

        if(!areaDeInteresseInstance) {
            flash.message = "AreaDeInteresse not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ areaDeInteresseInstance : areaDeInteresseInstance ]
        }
    }

    def update = {
        def areaDeInteresseInstance = AreaDeInteresse.get( params.id )
        if(areaDeInteresseInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(areaDeInteresseInstance.version > version) {
                    
                    areaDeInteresseInstance.errors.rejectValue("version", "areaDeInteresse.optimistic.locking.failure", "Another user has updated this AreaDeInteresse while you were editing.")
                    render(view:'edit',model:[areaDeInteresseInstance:areaDeInteresseInstance])
                    return
                }
            }
            areaDeInteresseInstance.properties = params
            if(!areaDeInteresseInstance.hasErrors() && areaDeInteresseInstance.save()) {
                flash.message = "AreaDeInteresse ${params.id} updated"
                redirect(action:show,id:areaDeInteresseInstance.id)
            }
            else {
                render(view:'edit',model:[areaDeInteresseInstance:areaDeInteresseInstance])
            }
        }
        else {
            flash.message = "AreaDeInteresse not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def areaDeInteresseInstance = new AreaDeInteresse()
        areaDeInteresseInstance.properties = params
        return ['areaDeInteresseInstance':areaDeInteresseInstance]
    }

    def save = {
        def areaDeInteresseInstance = new AreaDeInteresse(params)
        if(!areaDeInteresseInstance.hasErrors() && areaDeInteresseInstance.save()) {
            flash.message = "AreaDeInteresse ${areaDeInteresseInstance.id} created"
            redirect(action:show,id:areaDeInteresseInstance.id)
        }
        else {
            render(view:'create',model:[areaDeInteresseInstance:areaDeInteresseInstance])
        }
    }
}
